package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperations implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        //判断是否满
        int currSize = bookList.getUseSize();
        if(currSize == bookList.getBooks().length) {
            System.out.println("书架满了，放不了书了");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入添加的书名");
        String  name = scanner.nextLine();

        System.out.println("请输入添加的作者");
        String author = scanner.nextLine();

        System.out.println("请输入添加的类型");
        String type = scanner.nextLine();

        System.out.println("请输入添加的价格");
        int price = scanner.nextInt();

        Book newbook = new Book(name,author,price,type);
        //判断是否有这本书
        for (int i = 0; i < bookList.getUseSize(); i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)) {
                System.out.println("已经拥有这本书了");
                return;
            }
        }

        bookList.setBooks(currSize,newbook);

        bookList.setUseSize(currSize+1);

        System.out.println("新增成功");

    }
}
